﻿using System;
using RatingAuthor;
using System.Linq;

namespace ConditionTreeTestStand.Nodes
{
  public class SecondOperandEnumViewModel : SecondOperandViewModel<GenresSet>, ISingleSecondOperand
  {
    private readonly NodeContext _nodeContext;

    object ISingleSecondOperand.Value
    {
      get { return (long) SecondOperand; }
      set { SecondOperand = (long)value; }
    }

    public SecondOperandEnumViewModel(NodeViewModel owner, NodeContext nodeContext)
      : base(owner)
    {
      _nodeContext = nodeContext;
    }
    public string SecondOperandDisplayValue
    {
      get { return SecondOperand == 0 ? "∅":
        "{"  + string.Join(", ", SecondOperand.Lookup(_nodeContext.GenresLookup)) + "}"; }
    }
    protected override void OnSecondOperandChanged()
    {
      NotifyOfPropertyChange(() => SecondOperandDisplayValue);
      base.OnSecondOperandChanged();
    }
    public override string ToString()
    {
      return SecondOperandDisplayValue;
    }

    public void EditGanresPopup()
    {
      _nodeContext.WindowManager.ShowWindow(
        new SelectGenresViewModel(_nodeContext.GenresLookup, 
          SecondOperand, r => SecondOperand = r));
    }

    public override string StrValue
    {
      get { return string.Join(",", SecondOperand.Lookup(_nodeContext.GenresLookup)); }
      set 
      { 
        if (value == null) return;
        if (_nodeContext.GenresLookup == null) return;
        SecondOperand = _nodeContext.GenresLookup.Lookup(
          value.Split(new[]{','}, StringSplitOptions.RemoveEmptyEntries).
          Select(i => i.Trim()));
      }
    }
  }
}